#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <wiiuse/wpad.h>
#include <ogc/conf.h>
#include <ogc/pad.h>
#include <CheckRegion.h>

#include "network.h"
#include "tools.h"
#include "settings.h"
#include "filelog.h"

static const char *vwidescreen[] = {"Standard (4:3)", "Widescreen (16:9)"};
static const char *vsensorbar[] = {"Bottom", "Top"};
static const char *vsoundmode[] = {"Mono", "Stereo", "Surround"};
static const char *vshutdown[] = {"Full", "Idle"};
static const char *vsensitivity[] = {"Minimum", "Low", "Medium", "High", "Maximum"};
static const char *von_off[] = {"Disabled", "Enabled"};
static const char *veula[] = {"Not Accepted", "Accepted"};
static const char *vpassed[] = {"Not Completed", "Completed"};
static const char *vbootmode[] = {"Production", "Development"};
static const char *vdsaving[] = {"Disabled", "Enabled", "Debug"};
static const char *vlanguage[] = {"Japanse", "English", "German", "French", "Spanish", "Italian", "Dutch", "ChineseSimple", "ChineseTraditional", "Korean"};
static const char *vquestion[] = {	"What is your mother's maiden name?", 
									"What is your place of birth?", 
									"What is your favorite sports team?",
									"What was your favorite birthday present?", 
									"What's your favorite movie?",
									"If you could go anywhere, where would you go?"};

//---------------------------------------------------------------------------------
void SettingInfo(void) {
//---------------------------------------------------------------------------------
	PrintText("AREA");
	PrintText("MODEL");
	PrintText("DVD");
	PrintText("MPCH");
	PrintText("CODE");
	PrintText("SERNO");
	PrintText("VIDEO");
	PrintText("GAME");
	
	CHANGE_COLOR(YELLOW);
	printf("\nPress any key to continue");
	pressanykey();
	return;
}

//---------------------------------------------------------------------------------
s32 ParentalQuestion(void)
//---------------------------------------------------------------------------------
{
	int res;
	char buf[0x4A] = {0};

	res = CONF_Get("IPL.PC", buf, 0x4A);
	if(res < 0) return res;
	if(res!=0x4A) return CONF_EBADVALUE;
	
	return buf[7];
}

//---------------------------------------------------------------------------------
void SysConfInfo1(void) {
//---------------------------------------------------------------------------------
	s8 offset = 0;
	u32 bias = 0;	
	u8 shopcode = 0;
	u8 config1 = 0;
	u8 config2 = 0;
	int channels = 0;
	u8 motionplus = 0;

	CONF_GetShopCode(&shopcode);
	CONF_GetCounterBias(&bias);
	CONF_GetDisplayOffsetH(&offset);
	CB2Date(bias);
	CONF_Get("IPL.CD", &config1, sizeof(config1));
	CONF_Get("IPL.CD2", &config2, sizeof(config2));
	CONF_Get("IPL.INC", &channels, sizeof(channels));
	CONF_Get("MPLS.MOVIE", &motionplus, sizeof(motionplus));

	PrintValue1("IPL.IDL (Shutdown Mode)=", vshutdown, CONF_GetShutdownMode());
	PrintValue1("IPL.PGS (Progressive Scan)=", von_off, CONF_GetProgressiveScan());
	PrintValue1("IPL.E60 (PAL 576i)=", von_off, CONF_GetEuRGB60());
	PrintValue1("BT.SENS (IR Sensitivity)=", vsensitivity, CONF_GetIRSensitivity());
	PrintValue1("BT.BAR (Sensor Bar Position)=", vsensorbar, CONF_GetSensorBarPosition());
	PrintValue1("BT.SPKV (WPad Speaker Volume)=", NULL, CONF_GetPadSpeakerVolume());
	PrintValue1("BT.MOT (WPad Rumble)=", von_off, CONF_GetPadMotorMode());
	PrintValue1("IPL.SND (Sound Mode)=", vsoundmode, CONF_GetSoundMode());
	PrintValue1("IPL.LNG (Language)=", vlanguage, CONF_GetLanguage());
	printf(CYAN "IPL.CB (Counter Bias)=%s%u (%s)\n", bias==0 ? RED : WHITE, bias, bias_time);
	Log("IPL.CB (Counter Bias)=%u (%s)\r\n", bias, bias_time);
	PrintValue1("IPL.SSV (Burn-in Reduction)=", von_off, CONF_GetScreenSaverMode());
	PrintValue1("IPL.DH (Display Offset)=", NULL, offset);
	PrintValue1("IPL.AR (Aspect Ratio)=", vwidescreen, CONF_GetAspectRatio());
	PrintValue1("IPL.EULA (EULA Accepted)=", veula, CONF_GetEULA());
	PrintValue1("NET.WCFG (WiiConnect24)=", von_off, CONF_GetWiiConnect24());
	PrintValue1("IPL.SADR (Shop Code)=", CONF_CountryCodes, shopcode);
	PrintValue1("IPL.CD (Initial Setup)=", vpassed, config1);
	PrintValue1("IPL.CD2 (Network Setup)=", vpassed, config2);
	PrintValue1("IPL.INC (Installed Channels)=", NULL, channels);
	PrintValue1("MPLS.MOVIE (Motion Plus Instructional Video)=", vpassed, motionplus);
	
	CHANGE_COLOR(YELLOW);
	printf("\nNote: If some values return -0x6002, that just means you've never set them");
	printf("\n\nPress any key to continue");
	pressanykey();
	return;
}

//---------------------------------------------------------------------------------
void SysConfInfo2(void) {
//---------------------------------------------------------------------------------
	u8 nickname[0x16] = {0};
	s8 answer[32] = {0};
	s8 pin[5] = {0};
	
	CONF_GetParentalPassword(pin);
	CONF_GetNickName(nickname);
	CONF_GetParentalAnswer(answer);
	
	PrintValue2("IPL.NIK (Nickname)=", (s8*) nickname, MAX_CHARACTERS(nickname));
	PrintValue2("IPL.PC (Parental PIN)=", pin, MAX_CHARACTERS(pin));
	PrintValue1("IPL.PC (Parental Question)=", vquestion, ParentalQuestion());
	printf("\n");
	PrintValue2("IPL.PC (Parental Answer)=", answer, MAX_CHARACTERS(answer));
	
	CHANGE_COLOR(YELLOW);
	printf("\nNote: If some values return -0x6002, that just means you've never set them");
	printf("\n\nPress any key to continue");
	pressanykey();
	return;
}

//---------------------------------------------------------------------------------
void SysConfInfo3(void) {
//---------------------------------------------------------------------------------
	s32 ret;
	u8 bootmode = 0;
	u8 devVIM = 0;
	u8 devCTC = 0;
	u8 drivesaving = 0;
	u8 wwwrestriction = 0;
	u32 ncrestriction = 0;

	ret = CONF_Get("DEV.BTM", &bootmode, sizeof(bootmode));
	PrintValue1("DEV.BTM (Boot Mode)=", vbootmode, bootmode + ret);

	ret = CONF_Get("DEV.VIM", &devVIM, sizeof(devVIM));
	PrintValue1("DEV.VIM (Unknown purpose)=", NULL, devVIM + ret);

	ret = CONF_Get("DEV.CTC", &devCTC, sizeof(devCTC));
	PrintValue1("DEV.CTC (Unknown purpose)=", NULL, devCTC + ret);

	ret = CONF_Get("DEV.DSM", &drivesaving, sizeof(drivesaving));
	PrintValue1("DEV.DSM (Drive Saving Mode)=", vdsaving, drivesaving + ret);

	//ret = CONF_Get("DVD.CNF", &dvdCNF, sizeof(dvdCNF));
	//PrintValue1("DVD.CNF ()=", NULL, dvdCNF + ret);

	ret = CONF_Get("WWW.RST", &wwwrestriction, sizeof(wwwrestriction));
	PrintValue1("WWW.RST (WWW Restriction)=", NULL, wwwrestriction + ret);

	//ret = CONF_Get("NET.CNF", &netCNF, sizeof(netCNF));
	//PrintValue1("NET.CNF ()=", NULL, netCNF + ret);

	//ret = CONF_Get("NET.CFG", &netCFG, sizeof(netCFG));
	//PrintValue1("NET.CFG ()=", NULL, netCFG + ret);

	ret = CONF_Get("NET.CTPC", &ncrestriction, sizeof(ncrestriction));
	PrintValue1("NET.CTPC (Net Content Restrictions)=", NULL, ncrestriction + ret);

	CHANGE_COLOR(YELLOW);
	printf("\nNote: If some values return -0x6002, that just means you've never set them");
	printf("\n\nPress any key to continue");
	pressanykey();
	return;
}

//---------------------------------------------------------------------------------	
void BTInfo(void) {
//---------------------------------------------------------------------------------
	conf_pads pads;
	CONF_GetPadDevices(&pads);
	pads.num_registered *= (pads.num_registered > 0); // Ensure that pads.num_registered isn't negative
	printf(CYAN "Remotes Registered: " WHITE "%i\n", pads.num_registered);
	Log("Remotes Registered: %i\r\n", pads.num_registered);
	u8 i;
	for(i=0; i<pads.num_registered; i++) {
		printf(CYAN "Remote %u: " WHITE "%s\n", i + 1, pads.registered[i].name);
		Log("Remote %u: %s\r\n", i + 1, pads.registered[i].name);
	}
	
	CHANGE_COLOR(YELLOW);
	printf("\nPress any key to continue");
	pressanykey();
	return;
}
